package com.nuo.backend.common.utils;

import org.jasypt.encryption.pbe.PooledPBEStringEncryptor;
import org.jasypt.encryption.pbe.config.SimpleStringPBEConfig;

/**
 * @description: TODO .yml 加密
 * @author nuo
 * @date 2022/12/10 17:40
 * @version 1.0
 */
public class YmlEncryptUtil {

    private static final PooledPBEStringEncryptor encryptor;
    private static final String ALGORITHM = "PBEWITHHMACSHA512ANDAES_256";
    
    /**
     * 与该脚手架无关
     */
    private static final String SALT = "test";

    static {
        encryptor = new PooledPBEStringEncryptor();
        SimpleStringPBEConfig config = new SimpleStringPBEConfig();
        // 指定加密算法
        config.setAlgorithm(ALGORITHM);
        // 加密盐值
        config.setPassword(SALT);
        config.setKeyObtentionIterations("1000");
        config.setPoolSize("1");
        config.setProviderName("SunJCE");
        config.setSaltGeneratorClassName("org.jasypt.salt.RandomSaltGenerator");
        config.setIvGeneratorClassName("org.jasypt.iv.RandomIvGenerator");
        config.setStringOutputType("base64");
        encryptor.setConfig(config);
    }

    public static void main(String[] args) {
        // FLrdKTPKqkQByMONylfRBWndwnlHM+70VGzvOxlM6DuVlrFx2HghJOKWN+klYkiT
        System.out.println(encrypt("test"));
        // test
        System.out.println(decrypt("FLrdKTPKqkQByMONylfRBWndwnlHM+70VGzvOxlM6DuVlrFx2HghJOKWN+klYkiT"));
    }

    /**
     * @description: 加密
     * @param pwd 原密码
     * @return java.lang.String
     * @date: 2022/12/10 18:24
     */
    public static String encrypt (String pwd) {
        return encryptor.encrypt(pwd);
    }

    /**
     * @description: 解密
     * @param msg 密文
     * @return java.lang.String
     * @date: 2022/12/10 20:07
     */
    public static String decrypt (String msg) {
        return encryptor.decrypt(msg);
    }

}
